import {createApp} from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//1.引入element-plus组件库
import ElementPlus, {ElMessage} from 'element-plus'
import 'element-plus/dist/index.css'
//3.引入element-plus图标库
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//5.1修改elementPlus默认中文
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import axios from "axios";

//8.1配置根路径
const BASE_URL = 'http://localhost:8080';
//window里配置的都是全局内容，可以在任意script标签内部使用
window.BASE_URL = BASE_URL;

const app = createApp(App)
//8.2给app也做全局根路径配置，方便在template标签中使用
app.config.globalProperties.BASE_URL = BASE_URL;

//11.开启跨域携带 Cookie
window.axios = axios;
axios.defaults.withCredentials = true
//12.替换全局axios  注意之后不需要在自己的前端vue中直接使用axios即可  无需再次引入
app.config.globalProperties.$axios = axios;

//13.axios相应拦截器,处理Security的响应状态码
axios.interceptors.response.use(
    response => response,
    error => {
        console.log(error);
        ElMessage.error(error.response.data.msg);
        //授权失败的时候重新加载页面  会清空user对象信息  然后结合index.js会跳转到login
        // /*index.js
        router.beforeEach((to, from, next) => {
            let user = localStorage.user;
            if (to.path !== '/login' && !user && to.path !== '/')
                next({path: '/login', query: {redirect: to.fullPath}}, ElMessage.error('请先登录'))
            else
                next()
        })
        // * */
        if (error.response.status == 401 || error.response.status == 403) {
            localStorage.removeItem('user');
            localStorage.removeItem('breadcrumb');
            window.location.reload()
        } else if (error.response.data.path.indexOf('login') != -1 && error.response.status == 404) {
            window.location.reload()
        }
    }
)

//4.遍历ElementPlusIconsVue对象中的所有图标组件并注册到app中
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

//2.app.use(ElementPlus) 使用Element-plus组件库
//5.2 app.use(ElementPlus,{ locale: zhCn })将El本地化为中文
app.use(ElementPlus, {locale: zhCn}).use(store).use(router).mount('#app')

//6.解决ResizeObserver Error
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    }
}
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
}

//7.配置获取当前登陆用户的全局方法
window.getUser = () => {
    //如果获取到了用户数据，转回JS对象
    //如果获取不到用户数据，Boolean('')是false，得到null
    return localStorage.user ? JSON.parse(localStorage.user) : null
}

//9.创建全局方法，用来获取指定code对应的字典项
window.loadDictOptions = (object, dictCode) => {
    //向后端发请求获取字典项
    axios.get(BASE_URL + '/v1/dictoption/select/' + dictCode).then((response) => {
        if (response.data.code == 2000) {
            //将字典项保存到对象中
            object.value = response.data.data;
        } else {
            ElMessage.error(response.data.msg);
        }
    })
}